<!--车队管理员独立开智能钥匙柜页面-->
<template>
	<view class="page_all">
		<view class="page_titleArea">
			<view class="page_title_line">
				<view class="page_title_line_left" @click="leftClick()"><image src="../../static/all/back_white.png" style="width:50rpx;height:50rpx;"></image></view>
				<view class="page_title_line_middle">开智能钥匙柜</view>
				<view class="page_title_line_left"></view>
			</view>
		</view>
		<view class="page_contentArea" style="background-color:#F7F7F7;">
			<view class="space_55"></view>
			<view class="key_line">
				<view class="key_line_item" :style="item.remark" v-for="(item,index) in keyBoxList" :key="index"
				      @click="openKeyBox(item.status,item.cabinetName)">
					<view class="key_line_item_1">{{item.boxCode}}号柜</view>
					<view class="key_line_item_2">{{item.text}}</view>
				</view>
				<!-- <view class="key_line_item" style="background-color:#ECA93E;">
					<view class="key_line_item_1">A_02</view>
					<view class="key_line_item_2">已打开</view>
				</view>
				<view class="key_line_item" style="background-color:#E51410;">
					<view class="key_line_item_1">A_03</view>
					<view class="key_line_item_2">故障</view>
				</view>
				<view class="key_line_item" style="background-color:#ECA93E;">
					<view class="key_line_item_1">B_01</view>
					<view class="key_line_item_2">已打开</view>
				</view>
				<view class="key_line_item" style="background-color:#E51410;">
					<view class="key_line_item_1">B_02</view>
					<view class="key_line_item_2">故障</view>
				</view> -->
			</view>
			<view class="space_80"></view>
		</view>
	</view>
</template>

<script>
	import utils from "@/utils/business/services.js";
	export default {
		data() {
			return {
				keyBoxList:[], //钥匙柜列表
				
				//在此之上补充变量
			}
		},
		watch:{},
		onLoad(){
			this.getKeyBoxList();
		},
		onShow(){},
		methods:{
			/******************************以下是通用方法*********************************/
			//顶部导航回退箭头点击触发
			leftClick(){
				utils.pageBack();
			},
			/******************************以上是通用方法**********************************/
			
			/*************************以下是智能钥匙柜相关方法*****************************/
			getKeyBoxList(){
				var that=this;
				utils.getKeyBoxList().then(res => {
					console.log('获取钥匙柜列表返回值==',res); 
					if(res.data.status=="success"){
						that.keyBoxList=res.data.rows;
						for(var i=0;i<that.keyBoxList.length;i++){
							console.log("第"+i+"号柜子的boxCode与status==",that.keyBoxList[i].boxCode,that.keyBoxList[i].status);
							that.keyBoxList[i].cabinetName=that.keyBoxList[i].boxCode;
							that.keyBoxList[i].boxCode=that.keyBoxList[i].boxCode.split(":")[1];
							if(that.keyBoxList[i].status=="close"){
								that.keyBoxList[i].text="未打开";
								that.keyBoxList[i].remark="background-color:#0475FF;";
							}
							if(that.keyBoxList[i].status=="open"){
								that.keyBoxList[i].text="已打开";
								that.keyBoxList[i].remark="background-color:#ECA93E;";
							}
							if(that.keyBoxList[i].status=="error"){ //预留
								that.keyBoxList[i].text="故障";
								that.keyBoxList[i].remark="background-color:#E51410;";
							}
						}
					}
					if(res.data.status=="error"){
						let msg=res.data.message;
						uni.navigateTo({
							url:'/pages/error/error?errorMsg='+msg 
						})
					}
				}).catch(error => {
					uni.navigateTo({
						url:'/pages/error/error?errorMsg='+error
					})
				})
			},
			//开指定的柜门
			openKeyBox(status,cabinetName){
				if(status=="close"){
					//关闭状态下可以打开 
					var devCode=cabinetName.split(":")[0];
					var boxNum=cabinetName.split(":")[1];
					console.log("需要打开的柜子接口参数==",devCode,boxNum);
					this.sendMQTTMsg(devCode,boxNum);
				}
			},
			sendMQTTMsg(devCode,boxNum){
				var that=this;
				var obj={};
				obj.devCode=devCode;
				obj.boxNum=boxNum;
				utils.sendMQTTMsg(obj).then(res => {
					console.log('开指定柜门接口返回值==',res); 
					//先给个提示
					uni.showModal({
						title: '提示',
						content: '您已打开'+boxNum+'号柜！',
						showCancel:false,
						success: function (res) {
							if(res.confirm){} 
						}
					}); //uni.showModal结束
					//再置为已打开
					if(res.status==200){
						//需将打开的柜子的状态置为open
						for(var i=0;i<that.keyBoxList.length;i++){
							if(that.keyBoxList[i].boxCode==boxNum){
								that.keyBoxList[i].status="open";
								that.keyBoxList[i].text="已打开";
								that.keyBoxList[i].remark="background-color:#ECA93E;";
								break;
							}
						}
					}
					// if(res.status==500){
					// 	let msg=res.data.message;
					// 	uni.navigateTo({
					// 		url:'/pages/error/error?errorMsg='+msg 
					// 	})
					// }
				}).catch(error => {
					uni.navigateTo({
						url:'/pages/error/error?errorMsg='+error
					})
				})
			},
			/*************************以下是智能钥匙柜相关方法*****************************/
			
			/*********************在此之上补充方法**********************/
		},
	}
</script>
<style lang="scss">
@import '../../common/css/page.css'; 
@import '../../common/css/c_door.css';
</style>